<?php
namespace app\admin\model;
use think\Session;
/*
 *表名
 */
class AdminModel extends BaseModel
{
    protected $table = 'xst_admin';
    protected $rule = [
        'id' => '',
    ];
    protected $msg = [
        'id' => '',
    ];
    public function checkLoginUser()
    {
        $account = input('account');
        $pwd = input('pwd');
        $captcha = input('captcha');
        if(!captcha_check($captcha)){
            $status = ['code'=>0,'msg'=>'请输入正确的验证码'];
        }
        else if(!empty($account) && !empty($pwd)){
            $where = ['account'=>$account,'pwd'=>md5($pwd)];
            $result = $this->getInfo($where,'*');
            if(!empty($result)){
                $admin_id = Session::set('admin_id',$result['id']);
                Session::set('isadmin',$result['isadmin']);
                $status = ['code'=>1,'msg'=>'登录成功'];
            }else{
                $status = ['code'=>0,'msg'=>'用户名或密码不正确'];
            }

        }else{
            $status = ['code'=>0,'msg'=>'用户名和密码不能为空'];
        }
        return $status;
    }
    //查询所有管理员并分页
    public function AllData(){
        $list=$this->order('isadmin')->paginate(10);
        $page = $list->render();
        $arr=[$list,$page];
        return $arr;
    }
    //查询所有普通管理员不分页
    public function AllDataNF(){
        $arr=$this->where('isadmin',0)->order('isadmin')->select();
        return $arr;
    }
    //添加管理员
    public function add($data){
        $result =  $this->save(['account'=>$data['account'],'pwd'=>md5($data['pwd']),'isadmin'=>$data['isadmin']]);
        if ($result)
            $status = ['code'=>1,'msg'=>'添加成功'];
        else
            $status = ['code'=>0,'msg'=>'添加失败'];
        return $status;
    }
    //查询一个管理员的信息
    public function OneData($id){
        $res=$this->where('id',$id)->find();
        $res['groupid']=explode(',',$res['groupid']);
        return $res;
    }
    //修改管理员信息
    public function upxx($data){
        $result =  $this->where('id',$data['id'])->update(['isadmin'=>$data['isadmin']]);
        if ($result)
            $status = ['code'=>1,'msg'=>'修改成功'];
        else
            $status = ['code'=>0,'msg'=>'修改失败'];
        return $status;
    }
    //修改管理员权限组
    public function upgroups($id,$groupid){
        $result =  $this->where('id',$id)->update(['groupid'=>$groupid]);
        if ($result)
            $status = ['code'=>1,'msg'=>'修改成功'];
        else
            $status = ['code'=>0,'msg'=>'修改失败'];
        return $status;
    }
    //验证当前登录的管理员有无权限访问该控制器
    public function isadmin($id,$title){
        //查询该管理员的管理组id
        $res=$this->where('id',$id)->find();
        $groupids=explode(',',$res['groupid']);
        //查询该管理员的规则id
        $GroupModel=new GroupModel();
        $res=$GroupModel->where(['id'=>['in',$groupids],'status'=>['=','1']])->select();
        //把查询出来的规则ID放到一个字符串里
        $rules="0";
        for ($i=0;$i<count($res);$i++){
            $rules.=','.$res[$i]['rulesid'];
        }
        //规则id处理成数组
        $rulesids=explode(",",$rules);
        //根据标识符和id查询有无此权限
        $RulesModel=new RulesModel();
        $res=$RulesModel->where(['id'=>['in',$rulesids],'title'=>$title,'status'=>['=','1']])->find();
        return $res;
    }
    /**
     * 修改密码
     */
    public function loginedit()
    {
        $old_password = request()->post('old_password');
        $password = request()->post('password');
        $admin_id = Session::get('admin_id');
        $where = ['id'=>$admin_id];
        $result = $this->getInfo($where,'*');
        if(md5($old_password )== $result['pwd']) {
            $password = md5($password);
            $data = $this->where('id',$admin_id)->update(['pwd'=>$password]);
            if($data)
            {
                $status = ['code'=>1,'msg'=>'修改成功'];
            }else{
                $status = ['code'=>0,'msg'=>'修改失败'];
            }
        }else{
            $status = ['code'=>0,'msg'=>'旧密码错误'];
        }
        return $status;
    }
}
